package com.small.account.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.common.basis.result.Result;
import com.common.basis.result.ResultPage;
import com.common.basis.result.ResultSet;
import com.small.account.entity.AuPermission;
import com.small.account.entity.OauthClientDetails;
import com.small.account.entity.vo.OauthClientDetailsQ;
import com.small.account.service.IAuPermissionService;
import com.small.account.service.IOauthClientDetailsService;
import lombok.AllArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.OverridesAttribute;
import java.time.LocalDateTime;


/**
 * 客户端详细信息
 *
 * @author wangguochao
 * @date 2021-01-28
 */
@RestController
@AllArgsConstructor
@RequestMapping("/oauth-client-details")
public class OauthClientDetailsController {


    private final IOauthClientDetailsService iOauthClientDetailsService;

    private final BCryptPasswordEncoder bCryptPasswordEncoder;

    private final IAuPermissionService iAuPermissionService;

    /**
     * 查询全部
     */
    @GetMapping()
    public ResultPage<OauthClientDetails> findAll(OauthClientDetailsQ param) {
        LambdaQueryWrapper<OauthClientDetails> wrapper = Wrappers.lambdaQuery();
        wrapper.like(param.getClientId() != null, OauthClientDetails::getClientId, param.getClientId());
        IPage<OauthClientDetails> iPage = iOauthClientDetailsService.page(param.page(), wrapper);
        return ResultSet.page(iPage);
    }

    /**
     * 保存-新增
     */
    @PostMapping()
    public Result<?> save(@RequestBody @Validated OauthClientDetails param) {
        String clientSecret = bCryptPasswordEncoder.encode(param.getClientSecret());
        LambdaQueryWrapper<OauthClientDetails> query = Wrappers.lambdaQuery();
        query.eq(OauthClientDetails::getClientId, param.getClientId());
        int count = iOauthClientDetailsService.count(query);
        param.setClientSecret(clientSecret);
        boolean b;
        if (count > 0) {
            b = iOauthClientDetailsService.updateById(param);
        } else {
            AuPermission auPermission = new AuPermission();
            auPermission.setPermissionName(param.getAuthorities());
            auPermission.setSortTime(LocalDateTime.now());
            auPermission.setPermissionCode(param.getClientId());
            iAuPermissionService.save(auPermission);
            b = iOauthClientDetailsService.save(param);
        }
        return ResultSet.operation(b);
    }

    /**
     * 保存-修改
     */
    @PutMapping()
    public Result<?> modify(@RequestBody @Validated OauthClientDetails param) {
        String clientSecret = bCryptPasswordEncoder.encode(param.getClientSecret());
        param.setClientSecret(clientSecret);
        LambdaUpdateWrapper<OauthClientDetails> update = Wrappers.lambdaUpdate();
        update.eq(OauthClientDetails::getClientId, param.getClientId())
                .set(param.getScope() != null,
                        OauthClientDetails::getScope, param.getScope())
                .set(param.getRefreshTokenValidity() != null,
                        OauthClientDetails::getRefreshTokenValidity, param.getRefreshTokenValidity())
                .set(param.getResourceIds() != null,
                        OauthClientDetails::getResourceIds, param.getResourceIds())
                .set(param.getWebServerRedirectUri() != null,
                        OauthClientDetails::getWebServerRedirectUri, param.getWebServerRedirectUri())
                .set(param.getAdditionalInformation() != null,
                        OauthClientDetails::getAdditionalInformation, param.getAdditionalInformation())
                .set(param.getAuthorities() != null,
                        OauthClientDetails::getAuthorities, param.getAuthorities())
                .set(param.getClientId() != null,
                        OauthClientDetails::getClientId, param.getClientId())
                .set(param.getAuthorizedGrantTypes() != null,
                        OauthClientDetails::getAuthorizedGrantTypes, param.getAuthorizedGrantTypes())
                .set(param.getClientSecret() != null,
                        OauthClientDetails::getClientSecret, param.getClientSecret())
                .set(param.getAutoapprove() != null,
                        OauthClientDetails::getAutoapprove, param.getAutoapprove())
                .set(param.getAccessTokenValidity() != null,
                        OauthClientDetails::getAccessTokenValidity, param.getAccessTokenValidity());
        boolean b = iOauthClientDetailsService.update(update);
        return ResultSet.operation(b);
    }

    /**
     * 删除
     */
    @DeleteMapping()
    public Result<?> del(@RequestParam String clientId) {
        LambdaUpdateWrapper<OauthClientDetails> update = Wrappers.lambdaUpdate();
        update.eq(OauthClientDetails::getClientId, clientId);
        boolean b = iOauthClientDetailsService.remove(update);
        return ResultSet.operation(b);
    }
}

